#!/bin/bash
set -e
set -u

cd `dirname $0`

APP_BASE_DIR=/root/epcc
APP_NAME=epcc-209
APP_DIR=$APP_BASE_DIR/$APP_NAME
APP_RELEASE_DIR=$APP_BASE_DIR/$APP_NAME.releases/`date "+%Y-%m-%d.%H.%M.%S"`
APP_STARTUP_SCRIPT=$APP_DIR/scripts/startup.sh
APP_SHUTDOWN_SCRIPT=$APP_DIR/scripts/shutdown.sh

if [ -e $APP_SHUTDOWN_SCRIPT ];then
	chmod +x $APP_SHUTDOWN_SCRIPT
	echo "----------------------------------------"
	echo "exec command [$APP_SHUTDOWN_SCRIPT] to shutdown"
	echo "----------------------------------------"
	$APP_SHUTDOWN_SCRIPT
fi

mkdir -p $APP_RELEASE_DIR
unzip *.zip -d $APP_RELEASE_DIR

if [ -d $APP_DIR ];then 
	rm -rf $APP_DIR
fi
ln -s $APP_RELEASE_DIR $APP_DIR

chmod +x $APP_STARTUP_SCRIPT
echo "----------------------------------------"
echo "exec command [$APP_STARTUP_SCRIPT] to startup"
echo "----------------------------------------"
BUILD_ID=dontKillMe $APP_STARTUP_SCRIPT
